package com.xuebank.nfe.nfecar;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.licrafter.tagview.TagViewGroup;
import com.licrafter.tagview.views.ITagView;
import com.wzn.libaray.utils.Logger;
import com.wzn.libaray.utils.cache.SharedPreferencesUtil;
import com.xuebank.nfe.nfecar.activity.PreviewActivity;
import com.xuebank.nfe.nfecar.activity.ReleaseActivity;
import com.xuebank.nfe.nfecar.adapter.SimpleFragmentAdapter;
import com.xuebank.nfe.nfecar.anim.OptAnimationLoader;
import com.xuebank.nfe.nfecar.config.PictureConfig;
import com.xuebank.nfe.nfecar.entity.EventEntity;
import com.xuebank.nfe.nfecar.entity.LocalMedia;
import com.xuebank.nfe.nfecar.globals.Constants;
import com.xuebank.nfe.nfecar.model.DeleteTagPosition;
import com.xuebank.nfe.nfecar.model.ImagePathModel;
import com.xuebank.nfe.nfecar.model.Images;
import com.xuebank.nfe.nfecar.model.TagGroupModel;
import com.xuebank.nfe.nfecar.model.TagViewBodel;
import com.xuebank.nfe.nfecar.model.TagViewListModel;
import com.xuebank.nfe.nfecar.model.TagViewListModels;
import com.xuebank.nfe.nfecar.model.ViewPagerTagModel;
import com.xuebank.nfe.nfecar.observable.ImagesObservable;
import com.xuebank.nfe.nfecar.rxbus2.RxBus;
import com.xuebank.nfe.nfecar.rxbus2.Subscribe;
import com.xuebank.nfe.nfecar.rxbus2.ThreadMode;
import com.xuebank.nfe.nfecar.tools.ScreenUtils;
import com.xuebank.nfe.nfecar.tools.ToastManage;
import com.xuebank.nfe.nfecar.utils.DataRepo;
import com.xuebank.nfe.nfecar.view.TagEditDialog;
import com.xuebank.nfe.nfecar.view.TagImageView;
import com.xuebank.nfe.nfecar.widget.PreviewViewPager;
import com.yalantis.ucrop.UCrop;
import com.yalantis.ucrop.UCropMulti;
import com.yalantis.ucrop.model.CutInfo;



import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import rx.functions.Action1;


/**
 * author：luck
 * project：PictureSelector
 * package：com.luck.picture.ui
 * email：893855882@qq.com
 * data：16/12/31
 */
public class PicturePreviewActivity extends PictureBaseActivity implements
        View.OnClickListener, Animation.AnimationListener, SimpleFragmentAdapter.OnCallBackActivity,
        TagEditDialog.OnTagEditDialogClickListener  {
    private ImageView picture_left_back, wuyong;
    private TextView tv_img_num, tv_title, tv_ok;
    private PreviewViewPager viewPager;
    private LinearLayout id_ll_okss;
    private int position;
    private int tabPosition;
//    private LinearLayout ll_check;
    private List<LocalMedia> images = new ArrayList<>();
    private List<LocalMedia> imagess = new ArrayList<>();
    private List<LocalMedia> selectImages = new ArrayList<>();
    private TextView check;
    private SimpleFragmentAdapter adapter;
    private Animation animation;
    private boolean refresh;
    private int index;
    private int screenWidth;
    private Handler mHandler;
    private TagViewListModel mTagViewListModel = new TagViewListModel();
    private TagViewListModels mTagViewListModels = new TagViewListModels();
    private String ajoiwejw = "";
    String Imagepath;
    private ImagePathModel mImagePathModel;
    private DeleteTagPosition mMDeleteTagPosition =new DeleteTagPosition();

    LocalMedia image;
    private int qq=0;
    private int ww=0;
    private int mPosition;//viewpager当前的第几个子项

    private String Tag;

    private ImageButton mSaveBtn, mCreateBtn;
    private TagImageView mTagImageView;
    private TagEditDialog mDialog;
    private ImageView mImageView;

    private TagViewBodel mTagViewBodel;
    private TagGroupModel mTagGroupModel;

    private String eeeee="";
    int width=0;

    /**
     * EventBus 3.0 回调
     *
     * @param obj
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void eventBus(EventEntity obj) {
        switch (obj.what) {
            case PictureConfig.CLOSE_PREVIEW_FLAG:
                // 压缩完后关闭预览界面
                dismissDialog();
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        onBackPressed();
                    }
                }, 150);
                break;
        }
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.picture_preview);
        WindowManager wm1 = this.getWindowManager();
        width = wm1.getDefaultDisplay().getWidth(); ;


        viewPager = (PreviewViewPager) findViewById(R.id.preview_pager);

        if (!RxBus.getDefault().isRegistered(this)) {
            RxBus.getDefault().register(this);
        }

        if (getIntent().getParcelableExtra("ImageList") != null) {
            eeeee = getIntent().getStringExtra("eeeee");
//             TagViewListModels mTagViewListModelTwo = new TagViewListModels();
            TagViewListModel mTagViewListModelTwo = (TagViewListModel)getIntent().getParcelableExtra("ImageList");
            TagGroupModel model =(TagGroupModel)getIntent().getParcelableExtra("ImageListssssss");
            if (mTagViewListModelTwo != null) {
                for (int i=0;i<mTagViewListModelTwo.getTagViewBodel().size();i++) {

                    mTagViewBodel = new TagViewBodel();
                    if (mTagViewListModelTwo.getTagViewBodel().get(i).getTabText().size() != 0) {
                        for (int k=0; k < mTagViewListModelTwo.getTagViewBodel().get(i).getTabText().size();k++) {
                            mTagGroupModel = new TagGroupModel();

                            TagGroupModel.Tag mTag = new TagGroupModel.Tag();
                            mTag.setDirection(mTagViewListModelTwo.getTagViewBodel().get(i).getTabText().get(k).getTags().get(0).getDirection());
                            mTag.setName(mTagViewListModelTwo.getTagViewBodel().get(i).getTabText().get(k).getTags().get(0).getName());
                            mTag.setId(mTagViewListModelTwo.getTagViewBodel().get(i).getTabText().get(k).getTags().get(0).getId());
                            mTagGroupModel.getTags().add(mTag);
                            mTagGroupModel.setPercentX(mTagViewListModelTwo.getTagViewBodel().get(i).getTabText().get(k).getPercentX());
                            mTagGroupModel.setPercentY(mTagViewListModelTwo.getTagViewBodel().get(i).getTabText().get(k).getPercentY());

                            mTagViewBodel.getTabText().add(mTagGroupModel);

                        }

                    }



                    mTagViewBodel.setPath(mTagViewListModelTwo.getTagViewBodel().get(i).getPath());
                    mTagViewListModel.getTagViewBodel().add(mTagViewBodel);


                    Logger.d("sefwafqfr","mTagViewListModel    "+mTagViewListModel);
                    LocalMedia ssss = new LocalMedia();
                    ssss.setPath(mTagViewListModelTwo.getTagViewBodel().get(i).getPath());
                    ssss.setPosition(i);

                    images.add(ssss);
                }
            }

            position = mTagViewListModel.getTagViewBodel().size();

            Logger.d("mTagViewListModel","mTagViewListModel"+mTagViewListModel);
        }


        boolean is_bottom_preview = getIntent().
                getBooleanExtra(PictureConfig.EXTRA_BOTTOM_PREVIEW, false);

        if (is_bottom_preview) {
            // 底部预览按钮过来
            images = (List<LocalMedia>) getIntent().
                    getSerializableExtra(PictureConfig.EXTRA_PREVIEW_SELECT_LIST);

            for (int i=0;i<images.size();i++) {
                mTagViewBodel = new TagViewBodel();
                Bitmap bitmap = BitmapFactory.decodeFile(images.get(i).getPath());
                int bitmapW =bitmap.getWidth();
                bitmap.getHeight();
                int bitmapH =(int) ((bitmap.getWidth())*1.3);

                double height = bitmap.getWidth() * 1.3;
                if (bitmap.getWidth() * 1.3 > bitmap.getHeight()) {
                    height = bitmap.getHeight();
                } else {
                    height =bitmap.getWidth() * 1.3;
                }
                Bitmap jjjjj = Bitmap.createBitmap(bitmap,0,0,bitmapW,(int) height,null,false);
                jjjjj.getHeight();
                mTagViewBodel.setPath(saveBitmap(this,jjjjj));
                mTagViewListModel.getTagViewBodel().add(mTagViewBodel);
            }
            compressImageTwo(images);

        } else {
//            images = ImagesObservable.getInstance().readLocalMedias();
        }

        com.wzn.libaray.event.RxBus.getDefault().toObserverable(Images.class)
                .subscribe(new Action1<Images>() {
                    @Override
                    public void call(Images images) {
                        for (int i=0;i<images.getImages().size();i++) {
                            imagess.add(images.getImages().get(i));
                        }

                    }
                });


//        com.wzn.libaray.event.RxBus.getDefault().toObserverable(TagViewListModels.class)
//                .subscribe(new Action1<TagViewListModels>() {
//                    @Override
//                    public void call(TagViewListModels tagViewListModels) {
//                        mTagViewListModel = new  TagViewListModel();
//                        position = mTagViewListModel.getTagViewBodel().size();
//                        initViewPageAdapterData(mTagViewListModel);
//
//                        viewPager.notifyAll();
//                    }
//                });

        mHandler = new Handler();
        mCreateBtn = findViewById(R.id.createBtn);
        mDialog = new TagEditDialog(this, this);
        mCreateBtn.setOnClickListener(this);
        screenWidth = ScreenUtils.getScreenWidth(this);
        animation = OptAnimationLoader.loadAnimation(this, R.anim.modal_in);
        animation.setAnimationListener(this);
        picture_left_back = (ImageView) findViewById(R.id.picture_left_back);
//        ll_check = (LinearLayout) findViewById(R.id.ll_check);
//        id_ll_ok = (LinearLayout) findViewById(R.id.id_ll_ok);
        id_ll_okss = (LinearLayout) findViewById(R.id.id_ll_okss);
//        check = (TextView) findViewById(R.id.check);
        picture_left_back.setOnClickListener(this);
        tv_ok = (TextView) findViewById(R.id.tv_ok);
        tv_ok.setOnClickListener(this);
        id_ll_okss.setOnClickListener(this);
        tv_img_num = (TextView) findViewById(R.id.tv_img_num);
        wuyong = (ImageView)findViewById(R.id.select_bar_image_wuyong);
        tv_title = (TextView) findViewById(R.id.picture_title);
        position = getIntent().getIntExtra(PictureConfig.EXTRA_POSITION, 0);


        tv_img_num.setSelected(config.checkNumMode ? true : false);

        selectImages = (List<LocalMedia>) getIntent().
                getSerializableExtra(PictureConfig.EXTRA_SELECT_LIST);




        ajoiwejw = getIntent().getStringExtra("hhaha");
        if (ajoiwejw != null) {

            Imagepath = getIntent().getStringExtra("Imagepath");
            mTagViewBodel = new TagViewBodel();
            Bitmap bitmap = BitmapFactory.decodeFile(Imagepath);
            double height = bitmap.getWidth() * 1.3;
            if (bitmap.getWidth() * 1.3 > bitmap.getHeight()) {
                height = bitmap.getHeight();
            } else {
                height =bitmap.getWidth() * 1.3;
            }
            Bitmap jjjjj = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(), (int) (height),null,false);
            jjjjj.getHeight();

            mTagViewBodel.setPath(saveBitmap(this,jjjjj));
            mTagViewListModel.getTagViewBodel().add(mTagViewBodel);
        }
        initViewPageAdapterData(mTagViewListModel);

        for (int i=0;i>position;i++) {
            viewPager.getChildAt(i).findViewById(R.id.preview_image);
        }




        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(final int position, float positionOffset, int positionOffsetPixels) {
//                isPreviewEggs(config.previewEggs, position, positionOffsetPixels);
                Logger.d("viewpager","当前第"+position+"个");
                mPosition = position;



            }

            @Override
            public void onPageSelected(int i) {
                position = i;

                tv_title.setText(position + 1 + "/" + images.size());
                LocalMedia media = images.get(position);
                index = media.getPosition();
                if (!config.previewEggs) {
                    if (config.checkNumMode) {
//                        check.setText(media.getNum() + "");
                        notifyCheckChanged(media);
                    }
                    onImageChecked(position);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }


    /**
     * 单选图片
     */
    private void singleRadioMediaImage() {
        if (selectImages != null
                && selectImages.size() > 0) {
            LocalMedia media = selectImages.get(0);
            RxBus.getDefault()
                    .post(new EventEntity(PictureConfig.UPDATE_FLAG,
                            selectImages, media.getPosition()));
            selectImages.clear();
        }
    }

    /**
     * 初始化ViewPage数据
     */
    private void initViewPageAdapterData(TagViewListModel mTagViewListModel) {


        tv_title.setText(position + 1 + "/" + mTagViewListModel.getTagViewBodel().size());
        adapter = new SimpleFragmentAdapter(mTagViewListModel, this, this);
        viewPager.setAdapter(adapter);
        if (TextUtils.equals(ajoiwejw, "hahaw")) {
            Logger.d("tag","是从照相进来的");
        } else {
            viewPager.setCurrentItem(position);
            onSelectNumChange(true);
            onImageChecked(position);
            if (mTagViewListModel.getTagViewBodel().size() > 0) {
                LocalMedia media = images.get(position);
                index = media.getPosition();
                if (config.checkNumMode) {
                    tv_img_num.setSelected(true);
//                check.setText(media.getNum() + "");
                    notifyCheckChanged(media);
                }
            }
        }

    }

    /**
     * 选择按钮更新
     */
    private void notifyCheckChanged(LocalMedia imageBean) {
        if (config.checkNumMode) {
//            check.setText("");
            for (LocalMedia media : selectImages) {
                if (media.getPath().equals(imageBean.getPath())) {
                    imageBean.setNum(media.getNum());
//                    check.setText(String.valueOf(imageBean.getNum()));
                }
            }
        }
    }

    /**
     * 更新选择的顺序
     */
    private void subSelectPosition() {
        for (int index = 0, len = selectImages.size(); index < len; index++) {
            LocalMedia media = selectImages.get(index);
            media.setNum(index + 1);
        }
    }

    /**
     * 判断当前图片是否选中
     *
     * @param position
     */
    public void onImageChecked(int position) {
        if (images != null && images.size() > 0) {
            LocalMedia media = images.get(position);
//            check.setSelected(isSelected(media));
        } else {
//            check.setSelected(false);
        }
    }


    /**
     * 更新图片选择数量
     */

    public void onSelectNumChange(boolean isRefresh) {
    }

    /**
     * 更新图片列表选中效果
     *
     * @param isRefresh
     */
    private void updateSelector(boolean isRefresh) {
        if (isRefresh) {
            EventEntity obj = new EventEntity(PictureConfig.UPDATE_FLAG, selectImages, index);
            RxBus.getDefault().post(obj);
        }
    }

    @Override
    public void onAnimationStart(Animation animation) {
    }

    @Override
    public void onAnimationEnd(Animation animation) {
        updateSelector(refresh);
    }

    @Override
    public void onAnimationRepeat(Animation animation) {
    }


    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.picture_left_back) {
            onBackPressed();
        }
        if (id == R.id.tv_ok) {
            // 如果设置了图片最小选择数量，则判断是否满足条件
            if (eeeee.equals("eeeee")) {

                for (int q =0;q<mTagViewListModel.getTagViewBodel().size();q++) {

                    TagViewBodel mTagViewBodel = new TagViewBodel();
                    if (mTagViewListModel.getTagViewBodel().get(q).getTabText().size() != 0) {

                    for (int i=0;i<mTagViewListModel.getTagViewBodel().get(q).getTabText().size();i++) {
                        TagGroupModel model = new TagGroupModel();
                        model.setPercentX(mTagViewListModel.getTagViewBodel().get(q).getTabText().get(i).getPercentX());
                        model.setPercentY(mTagViewListModel.getTagViewBodel().get(q).getTabText().get(i).getPercentY());
                        TagGroupModel.Tag mtag = new TagGroupModel.Tag();
                        mtag.setId(mTagViewListModel.getTagViewBodel().get(q).getTabText().get(i).getTags().get(0).getId());
                        mtag.setName(mTagViewListModel.getTagViewBodel().get(q).getTabText().get(i).getTags().get(0).getName());
                        mtag.setDirection(mTagViewListModel.getTagViewBodel().get(q).getTabText().get(i).getTags().get(0).getDirection());
                        model.getTags().add(mtag);
                        mTagViewBodel.getTabText().add(model);
                    }
                    }
                    mTagViewBodel.setPath(mTagViewListModel.getTagViewBodel().get(q).getPath());

                    mTagViewListModels.getTagViewBodel().add(mTagViewListModel.getTagViewBodel().get(q));
                }


                com.wzn.libaray.event.RxBus.getDefault().post(mTagViewListModels);

                this.finish();
            } else {
                if (TextUtils.equals(ajoiwejw, "hahaw")) {
                    Logger.d("tag", "是从照相进来的");
//                image = new  LocalMedia();
//                image.setPath(Imagepath);
//                selectImages = new ArrayList<>();
//                selectImages.add(image);
//                Bundle bundle = new Bundle();

                    mTagViewListModels.getTagViewBodel().add(mTagViewListModel.getTagViewBodel().get(0));
                    com.wzn.libaray.event.RxBus.getDefault().post(mTagViewListModels);

                    Intent intent = new Intent();
                    intent.putExtra("skjnkejnre",Imagepath);
//                startActivity(intent);
                    setResult(Activity.RESULT_OK,intent);
                    this.finish();
                } else {

                    int size = selectImages.size();
                    LocalMedia image = selectImages.size() > 0 ? selectImages.get(0) : null;
                    String pictureType = image != null ? image.getPictureType() : "";
                    if (config.minSelectNum > 0) {
                        if (size < config.minSelectNum && config.selectionMode == PictureConfig.MULTIPLE) {
                            boolean eqImg = pictureType.startsWith(PictureConfig.IMAGE);
                            @SuppressLint("StringFormatMatches") String str = eqImg ? getString(R.string.picture_min_img_num, config.minSelectNum)
                                    : getString(R.string.picture_min_video_num, config.minSelectNum);
                            ToastManage.s(mContext,str);
                            return;
                        }
                    }
                    if (config.enableCrop && pictureType.startsWith(PictureConfig.IMAGE)) {
                        if (config.selectionMode == PictureConfig.SINGLE) {
                            originalPath = image.getPath();
                            startCrop(originalPath);
                        } else {
                            // 是图片和选择压缩并且是多张，调用批量压缩
                            ArrayList<String> cuts = new ArrayList<>();
                            for (LocalMedia media : selectImages) {
                                cuts.add(media.getPath());
                            }
                            startCrop(cuts);
                        }
                    } else {
                        TagViewListModel   mTagViewListModelq = new TagViewListModel();
                        for (int i=0;i<selectImages.size();i++) {
                            mTagViewBodel = new TagViewBodel();
                            mTagViewBodel.setPath(selectImages.get(i).getPath());
                            mTagViewListModelq.getTagViewBodel().add(mTagViewBodel);
                        }

                        for (int q =0;q<mTagViewListModel.getTagViewBodel().size();q++) {

                            TagViewBodel mTagViewBodel = new TagViewBodel();
                            for (int i=0;i<mTagViewListModel.getTagViewBodel().get(q).getTabText().size();i++) {
                                TagGroupModel model = new TagGroupModel();
                                model.setPercentX(mTagViewListModel.getTagViewBodel().get(q).getTabText().get(i).getPercentX());
                                model.setPercentY(mTagViewListModel.getTagViewBodel().get(q).getTabText().get(i).getPercentY());
                                TagGroupModel.Tag mtag = new TagGroupModel.Tag();
                                mtag.setId(mTagViewListModel.getTagViewBodel().get(q).getTabText().get(i).getTags().get(0).getId());
                                mtag.setName(mTagViewListModel.getTagViewBodel().get(q).getTabText().get(i).getTags().get(0).getName());
                                mtag.setDirection(mTagViewListModel.getTagViewBodel().get(q).getTabText().get(i).getTags().get(0).getDirection());
                                model.getTags().add(mtag);
                                mTagViewBodel.getTabText().add(model);
                            }
                            Bitmap bitmap = BitmapFactory.decodeFile(mTagViewListModelq.getTagViewBodel().get(q).getPath());
                            double height =bitmap.getWidth() * 1.3;
                            if (bitmap.getWidth() * 1.3 > bitmap.getHeight()) {
                                height = bitmap.getHeight();
                            } else {
                                height =bitmap.getWidth() * 1.3;
                            }
                            Bitmap jjjjj = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(), (int) (height),null,false);
                            jjjjj.getHeight();
                            mTagViewBodel.setPath(saveBitmap(this,jjjjj));

                            mTagViewListModels.getTagViewBodel().add(mTagViewListModel.getTagViewBodel().get(q));
                        }




                        com.wzn.libaray.event.RxBus.getDefault().post(mTagViewListModels);
                        onResult(selectImages);

                    }
                }
            }


        }


        com.wzn.libaray.event.RxBus.getDefault().toObserverable(TagViewListModel.class)
                .subscribe(new Action1<TagViewListModel>() {
                    @Override
                    public void call(TagViewListModel tagViewListModel) {

                        mTagViewListModel = tagViewListModel;

                    }
                });
        if (id == R.id.saveButton) {
            setResult(RESULT_OK);
            DataRepo.tagGroupList = mTagImageView.getTagGroupModels();
            finish();
        }
        if (id == R.id.createBtn) {
            int[] l1 = {0, 0};
            wuyong.getLocationInWindow(l1);

            SharedPreferencesUtil.getInstance(mContext).applyInt(Constants.TAG_DETETE_TOP,l1[1]);
            SharedPreferencesUtil.getInstance(mContext).applyInt(Constants.TAG_DETETE_BOTTON,(l1[1]+wuyong.getHeight()));

            Log.d("图片的坐标","getLeft"+l1[0]+"getRight"+(l1[0]+wuyong.getWidth())+"getTop"+l1[1]+"getBottom"+ l1[1]+wuyong.getHeight());

            Intent intent = new Intent(this, PreviewActivity.class);
            startActivityForResult(intent,123);
//            mDialog.show();

        }

    }

    @Override
    public void onResult(List<LocalMedia> images) {
        RxBus.getDefault().post(new EventEntity(PictureConfig.PREVIEW_DATA_FLAG, images));
        // 如果开启了压缩，先不关闭此页面，PictureImageGridActivity压缩完在通知关闭
        if (!config.isCompress) {
//            onBackPressed();
        } else {
            showPleaseDialog();
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case UCropMulti.REQUEST_MULTI_CROP:
                    List<CutInfo> list = UCropMulti.getOutput(data);
                    setResult(RESULT_OK, new Intent().putExtra(UCropMulti.EXTRA_OUTPUT_URI_LIST,
                            (Serializable) list));
                    finish();
                    break;
                case UCrop.REQUEST_CROP:
                    if (data != null) {
                        setResult(RESULT_OK, data);
                    }
                    finish();
                    break;
                case 1000:
                    if (data != null) {
                        Tag = data.getStringExtra(Constants.LABEL);

                    }
                    break;
                case 123:
                    if (data != null) {

                        TagGroupModel sss = data.getParcelableExtra("aoiaoidj");
                        TagGroupModel.Tag ssss = data.getParcelableExtra("sbiwu");

                        mTagGroupModel = sss;
                        ww++;

                        TagGroupModel model  = new TagGroupModel();
                        model.setPercentX(sss.getPercentX());
                        model.setPercentY(sss.getPercentY());

                        TagGroupModel.Tag mTag = new TagGroupModel.Tag();
                        mTag.setDirection(sss.getTags().get(0).getDirection());
                        mTag.setName(sss.getTags().get(0).getName());
                            mTag.setId(sss.getTags().get(0).getId());

                        ViewPagerTagModel viewPagerTagModel = new ViewPagerTagModel();
                        model.getTags().add(mTag);

                        viewPagerTagModel.setPosition(mPosition);
                        mTagViewListModel.getTagViewBodel().get(mPosition).getTabText().add(model);
                        Logger.d("TagViewListModel","mTagViewListModel为"+mTagViewListModel);

                        com.wzn.libaray.event.RxBus.getDefault().post(viewPagerTagModel);
                        com.wzn.libaray.event.RxBus.getDefault().post(mTagViewListModel);


                        com.wzn.libaray.event.RxBus.getDefault().post(mTagGroupModel);
                        adapter.notifyDataSetChanged();
                        initViewPageAdapterData(mTagViewListModel);
                    }
                    break;

            }
        } else if (resultCode == UCrop.RESULT_ERROR) {
            Throwable throwable = (Throwable) data.getSerializableExtra(UCrop.EXTRA_ERROR);
            ToastManage.s(mContext,throwable.getMessage());
        } else if (resultCode == 123) {

        }
    }


    @Override
    public void onBackPressed() {
        closeActivity();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (RxBus.getDefault().isRegistered(this)) {
            RxBus.getDefault().unregister(this);
        }
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
            mHandler = null;
        }
        if (animation != null) {
            animation.cancel();
            animation = null;
        }
    }

    @Override
    public void onActivityBackPressed() {
        onBackPressed();
    }

    @Override
    public void onCancel() {
        mDialog.dismiss();
    }

    @Override
    public void onTagGroupCreated(TagGroupModel group) {
        mTagGroupModel = group;
          ww++;

//        TagGroupModel model  = new TagGroupModel();
//        model.setPercentX(group.getPercentX());
//        model.setPercentY(group.getPercentY());
//
//        TagGroupModel.Tag mTag = new TagGroupModel.Tag();
//        mTag.setDirection(group.getTags().get(0).getDirection());
//        mTag.setName(group.getTags().get(0).getName());
//
//        model.getTags().add(mTag);
//        ViewPagerTagModel viewPagerTagModel = new ViewPagerTagModel();
//        viewPagerTagModel.setPosition(mPosition);
//        mTagViewListModel.getTagViewBodel().get(mPosition).setTabText(model);
//        Logger.d("TagViewListModel","mTagViewListModel为"+mTagViewListModel);
//
//        com.wzn.libaray.event.RxBus.getDefault().post(viewPagerTagModel);
//        com.wzn.libaray.event.RxBus.getDefault().post(mTagViewListModel);
//        adapter.notifyDataSetChanged();
//        initViewPageAdapterData(mTagViewListModel);
//
//        com.wzn.libaray.event.RxBus.getDefault().post(mTagGroupModel);
    }


    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }
}
